var redisConfig = require('../config/config.js').redisConfig;

/**
 * redisHelper 工具类（适应场景：保持会话，开启一个redis不关闭，
 * 可以用连接池改进，只维护一条redis连接不安全，托管给连接池（测试网络情况、保持连接始终可用、释放机制）去维护连接）
 * @type {Function}
 * Vector Ho
 */
module.exports = redisHelper;

function redisHelper() {
    this.client.on('connect', function(error) {
        if (error) {
            console.log('redis connect error...\n %s', error);
        } else {
            console.log('redis connect success...');
        }
    });
}

// redisHelper.prototype.redis = require('redis');

// 此处勿用 this.redis来创建redis客户端，因为这里的 this 指向了module，并未指向redisHelper
redisHelper.prototype.client = require('redis').createClient(redisConfig);
redisHelper.prototype.expire = 24 * 60 * 60; // 过期时间，默认为一天


/**
 * 封装redis的set方法
 * @param key 键名
 * @param value 值
 * @param expire 过期时间
 * @param callback
 */
redisHelper.prototype.set = function(key, value, expire, callback) {
    var that = this;
    var size = arguments.length;

    if (size === 3 && typeof expire === 'function') { // 没传expire参数情况
        callback = expire;
    }

    that.client.set(key, value, function(error, reply) {
        if (size === 4 && typeof expire === 'number') {
            console.log('key expire: ' + expire);
            that.client.expire(key, expire); // 持久时间
        } else if (size === 4) {
            console.log('key expire: ' + that.expire);
            that.client.expire(key, that.expire); // 持久一天
        }
        callback(error, reply);
    });
}

/**
 * 封装redis的hset方法
 * @param hashTble hash字典
 * @param key 键名
 * @param value 值
 * @param expire 过期时间
 * @param callback
 */
redisHelper.prototype.hset = function(hashTable, key, value, expire, callback) {
    var that = this;
    var size = arguments.length;

    if (size === 4 && typeof expire === 'function') { // 没传expire参数情况
        callback = expire;
    }

    that.client.hset(hashTable, key, value, function(error, reply) {
        if (size === 5 && typeof expire === 'number') {
            console.log('key expire: %s', expire);
            that.client.expire(key, expire); // 持久时间
        } else if (size === 5 && typeof expire !== 'number' && expire !== 'forever') {
            console.log('key expire: ' + that.expire);
            that.client.expire(key, that.expire); // 持久一天
        }
        callback(error, reply);
    });
}

/**
 * 封装redis的hget方法
 * @param hashTble hash字典
 * @param key 键名
 * @param callback
 */
redisHelper.prototype.hget = function(hashTable, key, callback) {
    this.client.hget(hashTable, key, function(error, reply) {
        callback(error, reply);
    });
}

/**
 * 封装redis的get方法
 * @param key 需要获取的值的键名
 * @param callback
 */
redisHelper.prototype.get = function(key, callback) {
    this.client.get(key, function(error, reply) {
        callback(error, reply);
    });
}

/**
 * 封装redis的del方法
 * @param key 键名
 * @param callback
 */
redisHelper.prototype.delete = function(key, callback) {
    var client = this.client;

    client.del(key, function(error, reply) {
        /** 日志
        if(error) {
            var mongodbObj = new thisObj.Mongodb();
            var logJson = {
                'log':'redis del 出错', 'params':'', 'code':error.code,
                'fatal':error.fatal, 'message':error.message, 'create_time':thisObj.customeFunc.getNowFormatTime()
            };
            mongodbObj.insert('syslog', logJson, function(error, result){});
        }
        */
        callback(error, reply);
    });
}

/**
 * 封装redis的hdel方法
 * @param key 键名
 * @param callback
 */
redisHelper.prototype.hdel = function(hashTable, key, callback) {
    var client = this.client;

    client.hdel(hashTable, key, function(error, reply) {
        callback(error, reply);
    });
}

/**
 * 封装redis的quit方法
 */
redisHelper.prototype.close = function() {
    var client = this.client;

    client.on('end', function(error) {
        if (error) {
            console.log('redis close error');
        } else {
            console.log('redis closed.');
        }
    });
    client.quit();
}
